package otherapi.easyexcel.merge;

import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.google.common.collect.Lists;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2/3/2023 11:36 AM
 */
public class EasyExcelTest {

    public static void writeExcel() {
        // 写excel的路径，当前项目路径下
        String fileName = getPath();
        // 构建ExcelWriter
        ExcelWriter excelWriter = EasyExcelFactory.write(fileName).excelType(ExcelTypeEnum.XLSX).build();

        // 构建sheet
        WriteSheet writeSheet = EasyExcelFactory.writerSheet("模板1").head(DemoData.class).build();
        // 写sheet
        excelWriter.write(data1(), writeSheet);
        excelWriter.finish();
    }

    private static String getPath() {
        return System.getProperty("user.dir") + "/" + System.currentTimeMillis() + ".xlsx";
    }

    private static List<DemoData> data1() {
        List<DemoData> list = Lists.newArrayList();
        for (int i = 0; i < 3; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + 1);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        for (int i = 0; i < 3; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + 2);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        for (int i = 0; i < 4; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + 3);
            data.setDate(new Date());
            data.setDoubleData(0.57);
            list.add(data);
        }
        return list;
    }

    public static void main(String[] args) {
        writeExcel();
    }

}
